package com.hyt.it.ogt.kq.main.controller.gov;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.core.util.StringUtils;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.gov.config.swagger.ApiVersionConstant;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.gov.utils.GovPage;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.gov.model.dto.examiner.OrgTreeParamDTO;
import com.hyt.it.ogt.kq.service.gov.model.entity.DeleteKwExaminerParam;
import com.hyt.it.ogt.kq.service.gov.model.param.ExaminerAllocationParam;
import com.hyt.it.ogt.kq.service.gov.model.param.ExaminerParam;
import com.hyt.it.ogt.kq.service.gov.model.param.TaskExaminerParam;
import com.hyt.it.ogt.kq.service.gov.model.param.TimeRoomExaminerParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.examiner.KwExaminerVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.examiner.TimeRoomExaminerVO;
import com.hyt.it.ogt.kq.service.gov.service.IExaminerService;
import com.hyt.loginfo.model.OfficeUserVO;
import com.hyt.loginfo.model.Tree;
import com.hyt.loginfo.model.User;
import com.hyt.swagger.ApiVersion;
import io.seata.common.util.CollectionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 考务人员 前端控制器
 * </p>
 *
 * @author wangg
 * @since 2021-11-25
 */
@RestController
@Api(tags = {"126.考务人员接口"}, value = "考务人员")
@ApiSort(value = 126)
@RequestMapping("/kw/gov/kwExaminer")
@Slf4j
public class ExaminerController extends BaseController {

    @Resource
    private IExaminerService iExaminerService;

    @ApiOperation(
            value = "26.1 获取机构部门和部门人员树",
            notes = ""
    )
    @ApiOperationSort(value = 1)
    @GetMapping(value = "/getKwExaminerOrgTree")
    public ApiResponse<Object> getKwExaminerOrgTree(@RequestParam String taskId, @RequestParam(required = true) String timeId) {
        try {
            //参数校验
            if (StringUtils.isEmpty(taskId)) {
                return ApiResponse.builder().code(ResponseCode.PARAM_TASK_ID_CAN_NOT_NULL.getCode()).build();
            }
            List<Tree> result = iExaminerService.getKwExaminerOrgTree(taskId, timeId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).data(result).build();
        } catch (KqException i) {
            log.error("# 查询考务人员部门异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 查询考务人员部门异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_GET_EXAMINER_ORG.getCode()).build();
        }
    }

    @ApiOperation(
            value = "26.2 根据部门id查询用户分页数据",
            notes = ""
    )
    @ApiOperationSort(value = 2)
    @GetMapping(value = "/getOfficeUserPageSet")
    public ApiResponse<Object> getOfficeUserPageSet(@RequestParam String taskId, @RequestParam(required = true) String timeId, @RequestParam String deptId) {
        try {
            //参数校验
            if (StringUtils.isEmpty(taskId)) {
                return ApiResponse.builder().code(ResponseCode.PARAM_TASK_ID_CAN_NOT_NULL.getCode()).build();
            }
            List<OfficeUserVO> officeUserVOs = iExaminerService.getOfficeUserPageSet(taskId, timeId, deptId);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).data(officeUserVOs).build();
        } catch (KqException i) {
            log.error("# 查询机构用户分页数据异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 查询机构用户分页数据异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_GET_OFFICE_EXAMINER_PAGE.getCode()).build();
        }
    }

    @ApiOperation(
            value = "26.3 获取考务人员信息列表",
            notes = ""
    )
    @ApiOperationSort(value = 3)
    @GetMapping(value = "/getKwExaminerList")
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    public ApiResponse<Page<TimeRoomExaminerVO>> getKwExaminerList(GovPage<KwExaminerVO> pageParam, TimeRoomExaminerParam timeRoomExaminerParam) {
        try {
            //参数校验
            if (StringUtils.isEmpty(timeRoomExaminerParam.getTaskId())) {
                return ApiResponse.<Page<TimeRoomExaminerVO>>builder().code(ResponseCode.PARAM_TASK_ID_CAN_NOT_NULL.getCode()).build();
            }
            /*if (StringUtils.isEmpty(timeRoomExaminerParam.getTimeId())) {
                return ApiResponse.<Page<TimeRoomExaminerVO>>builder().code(ResponseCode.PARAM_TIME_ID_CAN_NOT_NULL.getCode()).build();
            }*/
            //Page<TimeRoomExaminerVO> timeRoomExaminerVOList = iExaminerService.getKwExaminerList(timeRoomExaminerParam);
            Page<TimeRoomExaminerVO> timeRoomExaminerVOList = iExaminerService.getKwExaminerListV2(pageParam,timeRoomExaminerParam);
            return ApiResponse.<Page<TimeRoomExaminerVO>>builder().code(ResponseCode.SUCCESS.getCode()).data(timeRoomExaminerVOList).build();
        } catch (KqException i) {
            log.error("# 获取考务人员信息列表异常: {}", i.getMessage());
            return ApiResponse.<Page<TimeRoomExaminerVO>>builder().code(i.getErrCode()).build();
        } catch (Exception e) {
            log.error("# 获取考务人员信息列表异常:", e);
            return ApiResponse.<Page<TimeRoomExaminerVO>>builder().code(ResponseCode.ERROR_GET_EXAMINER_USER.getCode()).build();
        }
    }

    @ApiOperation(
            value = "26.4 保存考务人员设置",
            notes = ""
    )
    @ApiOperationSort(value = 4)
    @GetMapping(value = "/saveKwExaminerListByTime")
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    public ApiResponse saveKwExaminerListByTime(TaskExaminerParam param) {
        try {
            /*
            if(bindingResult.hasErrors()){
                return ApiResponse.builder().code(ResponseCode.SUCCESS_30000).data(bindingResult.getAllErrors()).build();
            }
            */
            User user = getUser();
            //参数校验
            if (Objects.isNull(param) || StringUtils.isEmpty(param.getTaskId())) {
                return ApiResponse.builder().code(ResponseCode.PARAM_TASK_ID_CAN_NOT_NULL.getCode()).build();
            }
            /*if (StringUtils.isEmpty(timeRoomExaminerParam.getTimeId())) {
                return ApiResponse.builder().code(ResponseCode.PARAM_TIME_ID_CAN_NOT_NULL.getCode()).build();
            }*/
            iExaminerService.saveKwExaminerListByTimeV2(param,user);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 保存考务人员监考老师设置异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 保存考务人员监考老师设置异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_SAVE_EXAMINER_USER.getCode()).build();
        }
    }

    @ApiOperation(
            value = "26.5 修改考务人员信息",
            notes = ""
    )
    @ApiOperationSort(value = 5)
    @PostMapping(value = "/updateKwExaminerInfo")
    public ApiResponse updateKwExaminerInfo(@RequestBody TimeRoomExaminerVO timeRoomExaminerVO) {
        try {
            //参数校验
            if (StringUtils.isEmpty(timeRoomExaminerVO.getTaskId())) {
                return ApiResponse.builder().code(ResponseCode.PARAM_TASK_ID_CAN_NOT_NULL.getCode()).build();
            }
            if (StringUtils.isEmpty(timeRoomExaminerVO.getTimeId())) {
                return ApiResponse.builder().code(ResponseCode.PARAM_TIME_ID_CAN_NOT_NULL.getCode()).build();
            }
            if (StringUtils.isEmpty(timeRoomExaminerVO.getUserId())) {
                return ApiResponse.builder().code(ResponseCode.PARAM_USER_ID_CAN_NOT_NULL.getCode()).build();
            }
            if (CollectionUtils.isEmpty(timeRoomExaminerVO.getRoomList())) {
                return ApiResponse.builder().code(ResponseCode.PARAM_ROOM_INFO_CAN_NOT_NULL.getCode()).build();
            }
            iExaminerService.updateKwExaminerInfo(timeRoomExaminerVO);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 修改考务人员信息异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 修改考务人员信息异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_UPDATE_EXAMINER_USER.getCode()).build();
        }
    }

    @ApiOperation(
            value = "26.6 重置考务人员密码",
            notes = ""
    )
    @ApiOperationSort(value = 6)
    @PostMapping(value = "/resetKwExarminerPwd")
    public ApiResponse resetKwExarminerPwd(@RequestBody ExaminerParam examinerParam) {
        try {
            //参数校验
            if (StringUtils.isEmpty(examinerParam.getTaskId())) {
                return ApiResponse.builder().code(ResponseCode.PARAM_TASK_ID_CAN_NOT_NULL.getCode()).build();
            }
            if (StringUtils.isEmpty(examinerParam.getTimeId())) {
                return ApiResponse.builder().code(ResponseCode.PARAM_TIME_ID_CAN_NOT_NULL.getCode()).build();
            }
            if (StringUtils.isEmpty(examinerParam.getUserId())) {
                return ApiResponse.builder().code(ResponseCode.PARAM_USER_ID_CAN_NOT_NULL.getCode()).build();
            }
            iExaminerService.resetKwExarminerPwd(examinerParam);
            return ApiResponse.builder().code(ResponseCode.SUCCESS.getCode()).build();
        } catch (KqException i) {
            log.error("# 重置考务人员密码异常: {}", i.getMessage());
            return ApiResponse.builder().code(i.getErrCode()).data(i.getErrMsg()).build();
        } catch (Exception e) {
            log.error("# 重置考务人员密码异常:", e);
            return ApiResponse.builder().code(ResponseCode.ERROR_RESET_EXAMINER_USER.getCode()).build();
        }
    }

    @ApiOperation(
            value = "26.7 分配考务人员",
            notes = "35006:考务人员分配异常; 35008:当前批次下暂无考务人员; 35009:当前批次下暂无考场;"
    )
    @ApiOperationSort(value = 7)
    @PostMapping(value = "/examinerAllocation")
    public ApiResponse<Object> examinerAllocation(@Valid @RequestBody ExaminerAllocationParam param,
                                                  BindingResult bindingResult) {
        try {
            if (bindingResult.hasErrors()) {
                //分配考务人员参数校验错误
                return ApiResponse.builder()
                        .code(ResponseCode.SUCCESS.getCode())
                        .data(bindingResult.getAllErrors())
                        .build();
            }
            iExaminerService.examinerAllocation(param);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException k) {
            log.error("# 考务人员分配业务异常: {}", k.getMessage());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 考务人员分配异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.EXAMINER_ALLOCATION_EXCEPTION.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "26.8 获取机构部门树",
            notes = "8"
    )
    @ApiOperationSort(value = 8)
    @GetMapping(value = "/getKwOrgTree")
    public ApiResponse<Object> getKwOrgTree(@RequestParam @Valid OrgTreeParamDTO param,BindingResult bindingResult){
        try {
            if (bindingResult.hasErrors()) {
                //分配考务人员参数校验错误
                return ApiResponse.builder()
                        .code(ResponseCode.PARAM_TASK_ID_CAN_NOT_NULL.getCode())
                        .data(bindingResult.getAllErrors())
                        .build();
            }
            List<Tree> result = iExaminerService.getKwOrgTree(param);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(result)
                    .build();
        } catch (KqException k) {
            log.error("# 获取机构部门树异常: {}", k.getMessage());
            return ApiResponse.builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 获取机构部门树异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_GET_EXAMINER_ORG.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "26.9 获取机构下人员",
            notes = "9"
    )
    @ApiOperationSort(value = 9)
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @GetMapping(value = "/getKwExaminer")
    public ApiResponse<Page<KwExaminerVO>> getKwExaminer(Page<KwExaminerVO> pageParam, OrgTreeParamDTO param){
        try {
            Page<KwExaminerVO> result = iExaminerService.getKwExaminer(pageParam,param);
            return ApiResponse.<Page<KwExaminerVO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(result)
                    .build();
        } catch (KqException k) {
            log.error("# 获取机构部门树异常: {}", k.getMessage());
            return ApiResponse.<Page<KwExaminerVO>>builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 获取机构部门树异常:", e);
            return ApiResponse.<Page<KwExaminerVO>>builder()
                    .code(ResponseCode.ERROR_GET_EXAMINER_ORG.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "26.10 删除任务下监控人员",
            notes = "10"
    )
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ApiOperationSort(value = 10)
    @PostMapping(value = "/deleteKwExaminer")
    public ApiResponse<Object> deleteKwExaminer(@RequestBody @Validated DeleteKwExaminerParam param,BindingResult bindingResult){
        try {
            User user = getUser();
            if (bindingResult.hasErrors()) {
                //分配考务人员参数校验错误
                return ApiResponse.<Object>builder()
                        .code(ResponseCode.PARAM_TASK_ID_CAN_NOT_NULL.getCode())
                        .data(ResponseCode.PARAM_TASK_ID_CAN_NOT_NULL.getMsg())
                        .build();
            }
            Boolean result = iExaminerService.deleteKwExaminer(param.getTaskId(), param.getUserId(),user);
            return ApiResponse.<Object>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(result)
                    .build();
        } catch (KqException k) {
            log.error("# 获取机构部门树异常: {}", k.getMessage());
            return ApiResponse.<Object>builder()
                    .code(k.getErrCode())
                    .data(k.getMessage())
                    .build();
        } catch (Exception e) {
            log.error("# 获取机构部门树异常:", e);
            return ApiResponse.<Object>builder()
                    .code(ResponseCode.ERROR_DELETE_EXAMINER_ORG.getCode())
                    .data(e.getMessage())
                    .build();
        }
    }

    @ApiOperation(
            value = "26.11 获取当前任务绑定的监考员userid",
            notes = "11"
    )
    @ApiVersion(group = ApiVersionConstant.V2_4_10)
    @ApiOperationSort(value = 11)
    @GetMapping(value = "/queryTaskExaminer")
    public ApiResponse<List<String>> queryTaskExaminer(@RequestParam String taskId){
        try {
            //参数校验
            List<String> result = iExaminerService.queryTaskExaminer(taskId);
            return ApiResponse.<List<String>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(result)
                    .build();
        } catch (KqException k) {
            log.error("# 获取当前任务绑定的监考员userid异常: {}", k.getMessage());
            return ApiResponse.<List<String>>builder()
                    .code(k.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 获取当前任务绑定的监考员userid异常:", e);
            return ApiResponse.<List<String>>builder()
                    .code(ResponseCode.ERROR_QUERY_EXAMINER_ORG.getCode())
                    .build();
        }
    }

}
